package cn.anecansaitin.whimsy.common.effect.ow;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.network.NetworkHelper;
import cn.anecansaitin.whimsy.common.network.StunEffectPack;
import cn.anecansaitin.whimsy.common.register.ModEffects;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class Stun extends MobEffect {
    public Stun() {
        super(MobEffectCategory.HARMFUL, 0);
    }

    @Override
    public void onEffectStarted(LivingEntity livingEntity, int pAmplifier) {
        if (livingEntity instanceof ServerPlayer player) {
            player.getAbilities().setFlyingSpeed(0);
            NetworkHelper.toClientPlayer(player, new StunEffectPack(true));
        }
    }

    @SubscribeEvent
    public static void onEffectRemove(MobEffectEvent.Remove event) {
        if (event.getEffect() == ModEffects.STUN.get() && event.getEntity() instanceof ServerPlayer player) {
            player.getAbilities().setFlyingSpeed(0.05f);
            NetworkHelper.toClientPlayer(player, new StunEffectPack(false));
        }
    }
    @SubscribeEvent
    public static void onEffectExpired(MobEffectEvent.Expired event) {
        if (event.getEffectInstance().getEffect() == ModEffects.STUN.get() && event.getEntity() instanceof ServerPlayer player) {
            player.getAbilities().setFlyingSpeed(0.05f);
            NetworkHelper.toClientPlayer(player, new StunEffectPack(false));
        }
    }
}
